package com.itheima.utils;

import com.aliyun.oss.*;
import com.aliyun.oss.model.ObjectMetadata;
import com.aliyun.oss.model.PutObjectRequest;
import org.junit.Test;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

public class AliyunOSSUtil {
    public static String  endpoint = "http://oss-cn-beijing.aliyuncs.com";;
    public static String accessKeyId = "LTAI5tQ21K1V1VUANBBzEt4p";
    public static String accessKeySecret = "n9EKdnHqxjHtv8gV1HXTAQYb1MIRo2";
    public static String bucketName="itcasthealth-space-hezixiu";
    // 创建OSSClient实例。
    //    上传文件


    public static void uploadFileByPath(String filePath,String fileName) {
          OSS ossClient=new OSSClientBuilder().build(endpoint,accessKeyId,accessKeySecret);

        // 创建PutObjectRequest对象。
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, fileName, new File(filePath));

        // 如果需要上传时设置存储类型与访问权限，请参考以下示例代码。
         ObjectMetadata metadata = new ObjectMetadata();
//         metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard.toString());
        // metadata.setObjectAcl(CannedAccessControlList.Private);
        metadata.setContentType(getContentType(fileName.substring(fileName.lastIndexOf("."))));
         putObjectRequest.setMetadata(metadata);

        // 上传文件。
        ossClient.putObject(putObjectRequest);

        // 关闭OSSClient。
        ossClient.shutdown();
    }
    public static void uploadFileByStream(String fileName,InputStream inputStream) {
        OSS ossClient=new OSSClientBuilder().build(endpoint,accessKeyId,accessKeySecret);

        // 创建PutObjectRequest对象。

        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, fileName, inputStream);

        // 如果需要上传时设置存储类型与访问权限，请参考以下示例代码。
        ObjectMetadata metadata = new ObjectMetadata();
//         metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard.toString());
        // metadata.setObjectAcl(CannedAccessControlList.Private);
        metadata.setContentType(getContentType(fileName.substring(fileName.lastIndexOf("."))));
        putObjectRequest.setMetadata(metadata);

        // 上传文件。
        ossClient.putObject(putObjectRequest);

        // 关闭OSSClient。
        ossClient.shutdown();
    }
//    删除文件
    public static void deleteFile(String fileName){
        OSS ossClient=null;
        try {
            ossClient=new OSSClientBuilder().build(endpoint,accessKeyId,accessKeySecret);
            // 删除文件或目录。如果要删除目录，目录必须为空。
            ossClient.deleteObject("itcasthealth-space-hezixiu", fileName);
            System.out.println("删除成功");
        } catch (OSSException oe) {
            System.out.println("Caught an OSSException, which means your request made it to OSS, "
                    + "but was rejected with an error response for some reason.");
            System.out.println("Error Message:" + oe.getErrorMessage());
            System.out.println("Error Code:" + oe.getErrorCode());
            System.out.println("Request ID:" + oe.getRequestId());
            System.out.println("Host ID:" + oe.getHostId());
        } catch (ClientException ce) {
            System.out.println("Caught an ClientException, which means the client encountered "
                    + "a serious internal problem while trying to communicate with OSS, "
                    + "such as not being able to access the network.");
            System.out.println("Error Message:" + ce.getMessage());
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
    }
//    判断应该设置什么样的ContentType
    public static String getContentType(String FilenameExtension) {
        if (FilenameExtension.equalsIgnoreCase(".pdf")) {
            return "application/pdf";
        }
        if (FilenameExtension.equalsIgnoreCase(".bmp")) {
            return "image/bmp";
        }
        if (FilenameExtension.equalsIgnoreCase(".gif")) {
            return "image/gif";
        }
        if (FilenameExtension.equalsIgnoreCase(".jpeg") ||
                FilenameExtension.equalsIgnoreCase(".jpg") ||
                FilenameExtension.equalsIgnoreCase(".png")) {
            return "image/jpg";
        }
        if (FilenameExtension.equalsIgnoreCase(".html")) {
            return "text/html";
        }
        if (FilenameExtension.equalsIgnoreCase(".txt")) {
            return "text/plain";
        }
        if (FilenameExtension.equalsIgnoreCase(".vsd")) {
            return "application/vnd.visio";
        }
        if (FilenameExtension.equalsIgnoreCase(".pptx") ||
                FilenameExtension.equalsIgnoreCase(".ppt")) {
            return "application/vnd.ms-powerpoint";
        }
        if (FilenameExtension.equalsIgnoreCase(".docx"))
        {
            return "application/msword";
        }
        if (FilenameExtension.equalsIgnoreCase(".xml")) {
            return "text/xml";
        }
        return "image/jpg";
    }
    @Test
    public  void uuidLen(){
        System.out.println("5d6fd4bf-c49c-41f9-9464-f1a78c13f729".length());
    }
}
